package com.example.ytyproject.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.ytyproject.entity.User;
import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@RestController
@RequestMapping("/")
public class LoginController {

    @Autowired
    private UserService userService;


    public static void main(String[] args) {
        //
        LocalDate currentDate = LocalDate.now(); // 当前时间
        int year = currentDate.getYear();

        // 获取当前年的所有天数 如365天


        // 计算有多少周  用 365/7 = 52 代表有52周


        // 计算每月的周数 开始时间 结束时间 从 1玥1日开始计算

        List<JSONObject> list = new ArrayList<>();
        String startDateStr = "2024-01-01";  // 本周结束时间
        // 如果当前时间为周六或周日 则从周一开始计算
        LocalDate startDate = LocalDate.parse(startDateStr);
        LocalDate startDate1 = startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SATURDAY));
        LocalDate startDate2 = startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
        if (startDate == startDate1) {
            startDate = startDate.plusDays(2);
        }
        if (startDate == startDate2) {
            startDate = startDate.plusDays(1);
        }
        for (int i = 1; i <= 52; i++) {
            JSONObject jsonObject = new JSONObject();
            // 获取当前周一的日期
            LocalDate one = startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 获取当前周五的日期
            LocalDate five = startDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
            // 如果周五不属于当前月 则为下一个月
            jsonObject.put("month", "第" + one.getMonthValue() + "月");
            jsonObject.put("week", "第" + i + "周");
            jsonObject.put("startDate", one);
            jsonObject.put("endDate", five);
            list.add(jsonObject);
            startDate = five.plusDays(3);
        }
        for (JSONObject object : list) {
            System.out.println("*************" + object);
        }
    }

    @PostMapping("/login")
    public R login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/logout")
    @CrossOrigin
    public R logout() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("access_token", "bqddxxwqmfncffacvbpkuxvwvqrhln");
        return R.ok(jsonObject);
    }
}
